<template>
  <div>
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      style="width: 500px; margin: 2px"
    >
      <el-form-item label="培训标题" prop="tilite">
        <el-input v-model="form.tilite"></el-input>
      </el-form-item>
      <el-form-item label="文档类型" prop="document" style="width: 270px">
        <el-radio-group v-model="form.document">
          <el-radio :label="1">文档教程</el-radio>
          <el-radio :label="2">视频教程</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="学习时长" prop="learning">
        <el-input v-model="form.learning"></el-input>
      </el-form-item>
      <el-form-item label="封面图片" prop="url">
        <el-upload
          style="width: 100px"
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="上传文件">
        <el-upload
          style="width: 80px"
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
        >
          <el-button size="small" style="background-color: green; color: white"
            >添加文件</el-button
          >
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button style="background-color: green; color: white" @click="add"
          >保存</el-button
        >
        <el-button style="color: green" @click="list">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addTrain } from "@/api/SafeManage/Train";
export default {
  data() {
    return {
      form: {
        tilite: "",
        document: 1,
        learning: "",
        url: "1",
        isDelete: "",
        addtime: "2021-07-30",
        study: "100",
        unlearned: "18",
        isDelete: "true",
      },
      imageUrl: "",
      rules: {
        tilite: [
          { required: true, message: "请输入培训标题", trigger: "blur" },
        ],
        document: [
          { required: true, message: "请选择文档类型", trigger: "change" },
        ],
        learning: [
          { required: true, message: "请输入学习时长", trigger: "blur" },
        ],
        url: [{ required: true, message: "请选择封面图片", trigger: "change" }],
      },
    };
  },
  methods: {
    //添加
    add() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addTrain(this.form).then((res) => {
            if (res.data) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.$router.push("/SafeManage/Train");
            } else {
              this.$message.error("添加失败");
              return;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //跳转
    list() {
      this.$router.push("/SafeManage/Train");
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
  },
  created() {},
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>